﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TrainTicketBookingSystem
{
    
    public partial class ModifyPwdForm : Form
    {
        private User u = null;
        UserService userService = new UserService();
        public ModifyPwdForm()
        {
            InitializeComponent();
        }

        private void ModifyPwdForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            LoginFrom.loginFrom.Show();
        }

        private void btnVerification_Click(object sender, EventArgs e)
        {
            string Account = UserAccount.Text;
            string CertificateNum = UserCertificateNum.Text;

            if (!Verification(Account, CertificateNum))
            {
                MessageBox.Show("验证失败");
                return;
            }
            else
            {
                MessageBox.Show("验证成功");
            }
        }

        private bool Verification(string Account,string CertificateNum)
        {
            u = null;
            u = userService.FindUserByAccount(Account);
            if(u != null && u.CertificateNum == CertificateNum)
            {
                return true;
            }
            return false;
        }

        private void BtnModify_Click(object sender, EventArgs e)
        {
            if(u == null)
            {
                MessageBox.Show("您还未进行验证");
                return;
            }

            string NewPwd1 = NewPwd.Text;
            string NewPwd2 = TheNewPwd.Text;

            MessageBox.Show(Modify(NewPwd1, NewPwd2) ? "修改成功，请返回登录" : "修改失败，两次输入不一致");
        }

        private bool Modify(string NewPwd1,string NewPwd2)
        {
            if(NewPwd1 == NewPwd2)
            {
                u.Password = NewPwd1;
               userService.ReSetPassword(u);
                return true;
            }

            return false;
        }

        private void btnReturnLogin_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ModifyPwdForm_Load(object sender, EventArgs e)
        {

        }
    }
}
